package com.dycs.utils;


import java.util.ArrayList;
import java.util.List;

public class JsonCollection {



    public String getIntersection(List<String> list) {

        List<String> myList = null;
        if (list != null){
            //初始化myList
            myList = init(list);
            for (String str : list){
                myList = getIntersectionByOne(myList,getOutOfBracket(str));
            }
        }

        return jsonSplit(myList);
    }

    public List<String> getIntersectionByOne(List<String> myList, String str2) {

        List<String> list = new ArrayList<>();
        String keyValue;
        JsonKeyValueIterator iterator = new JsonKeyValueIterator(str2);
        while (iterator.hasNextKeyValue()){
            keyValue = keyValueFormat(iterator.getNextKeyValue());
            if (myList.contains(keyValue)){
                list.add(keyValue);
            }
        }
        return list;
    }

    /**
     * json集合取并集
     * @param list json字符串集合
     * @return
     */
    public String getUnion(List<String> list) {

        List<String> myList = null;
        if (list != null){
            myList = new ArrayList<>();
            for (String str : list){
                getUnionByOne(myList,getOutOfBracket(str));
            }

        }

        return jsonSplit(myList);
    }

    /**
     * 将单个json中 myList不存在的键值对 以字符串形式存入myList
     * @param myList json并集中的键值对集合
     * @param str2 新的json字符串
     */
    public void getUnionByOne(List<String> myList, String str2) {

        String keyValue;
        JsonKeyValueIterator iterator = new JsonKeyValueIterator(str2);
        while (iterator.hasNextKeyValue()){
            keyValue = keyValueFormat(iterator.getNextKeyValue());
            if (!myList.contains(keyValue)){
                myList.add(keyValue);
            }
        }
    }

    /**
     * 去除“{}”
     * @param str json字符串
     * @return
     */
    public String getOutOfBracket(String str) {

        int startIndex = str.indexOf('{'),endIndex = str.indexOf('}');
        if (startIndex < 0 || endIndex < 0){
            return null;
        }else {
            return str.substring(startIndex+1,endIndex).trim();
        }
    }

    public List<String> init(List<String> list) {

        List<String> myList = new ArrayList<>();
        String firstJson = getOutOfBracket(list.remove(0));
        JsonKeyValueIterator iterator = new JsonKeyValueIterator(firstJson);
        while (iterator.hasNextKeyValue()){
            myList.add(keyValueFormat(iterator.getNextKeyValue()));
        }
        return myList;
    }

    public String jsonSplit(List<String> list) {

        StringBuilder json = new StringBuilder();
        json.append('{');
        if (list != null){
            for (String str : list){
                json.append(str);
                json.append(',');
            }
            if (json.length() > 1){
                json.deleteCharAt(json.length()-1);
            }
        }
        json.append("}");
        return json.toString();
    }

    public String keyValueFormat(String str){
        int index = str.indexOf(':');
        String key = str.substring(0,index).trim();
        String value = str.substring(index+1).trim();
        return key + " : " + value;
    }



}
